import axios from "axios";

export const instace = axios.create({
  baseURL: "http://localhost:3000", 
  timeout: 5000, //超时时间
});

instace.interceptors.request.use(
  //config:该参数表示当前请求的配置对象
  (config) => {
    // if (config.url !== "/login") {   // 判断请求是否是登录接口
    //   config.headers.Jwt_token = localStorage.getItem("token"); // 如果不是登录接口，就给请求头里面设置token
    // }
    
    return config;
  },
  (err) => {
    return Promise.reject(err); //将错误消息挂到promise的失败函数上
  }
);

instace.interceptors.response.use(
  (response) => {

    return response; //这里的response就是请求成功后的res , response.data即是请求成功后回调函数内的参数res.data
  },
  (err) => {

    return Promise.reject(err); //将错误消息挂到promise的失败函数上
  }
);

//封装请求的api
const callapi = (method = "GET", url, data = {}) => {
  return instace({
    method,
    url,
    params: method === "GET"||method === "DELETE" ? data : {},
    data: method === "POST"|| method === "PUT"? data : {},
  });
};



//封装GET请求函数
export const getapi = (url, data) => callapi("GET", url, data);
export const delapi = (url, data) => callapi("DELETE", url, data);
export const postapi = (url, data) => callapi("POST", url, data);
export const putapi = (url, data) => callapi("PUT", url, data);